package com.tom.zx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tom.zx.entity.DecorationScheme;
import com.tom.zx.entity.User;
import com.tom.zx.service.IDecorationSchemeService;
import com.tom.zx.service.IUserService;
import com.tom.zx.vo.DecorationSchemeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Arrays;

/**
 * 装修方案 前端控制器
 *
 * @author lp
 * @since 2022-04-09
 */
@RestController
@RequestMapping("/zx/decorationScheme")
public class DecorationSchemeController {

  @Autowired private IDecorationSchemeService decorationSchemeService;
  @Autowired private IUserService userService;

  /** 查询分页列表 */
  @GetMapping("/page")
  public Page<DecorationScheme> loadPageContract(
      Page<DecorationScheme> page, DecorationScheme decorationScheme) {
    return decorationSchemeService.page(
        page,
        new LambdaQueryWrapper<DecorationScheme>()
            .like(DecorationScheme::getSchemeName, decorationScheme.getSchemeName()));
  }

  @GetMapping("page/user")
  public Page<DecorationScheme> loadPageUserContract(Principal principal,Page<DecorationScheme> page, DecorationScheme decorationScheme){
    final User user = userService.loadUserByUserName(principal.getName());

    return decorationSchemeService.page(
            page,
            new LambdaQueryWrapper<DecorationScheme>()
                    .like(DecorationScheme::getSchemeName, decorationScheme.getSchemeName())
                    .eq(DecorationScheme::getUserId,user.getId())
    );
  }

  /** 保存合同信息 */
  @PostMapping
  public boolean saveDecorationScheme(Principal principal, @RequestBody DecorationSchemeVo decorationScheme) {
    final User user = userService.loadUserByUserName(principal.getName());
    decorationScheme.setUserId(user.getId());
    return decorationSchemeService.saveOrUpdate(decorationScheme);
  }

  /** 删除数据 通过ids */
  @DeleteMapping
  public boolean deleteContractByIds(@RequestBody Long[] ids) {
    return decorationSchemeService.removeBatchByIds(Arrays.asList(ids));
  }
}
